from config import *
from help import *
from routin import *

def is_my_key(st,a):
    if a>=0:
        return True
    if 'caps' in st or scroll_str in st or pause_str in st or 'ctrl' in st or 'alt' in st:
        return True
    return False    

def is_p_key(st,a):
    if a in (11,15):
        return True
    if scroll_str in st or 'ctrl' in st or 'alt' in st:
        return True
    return False

def alt_ini(st):
    alt_ini_imp(st)

def scroll_ini():    
    cfg.pause_mode = True

def ctrl_ini(st):    
    cfg.ctrl_job=st
    cfg.ctrl_job_time= time.time()

def deal_no_mode(st,a):
    if a==11:
        pm.add = True
        pm.add_main = True
        pm.add_other = False        
        cfg.enter_pressed_seq = []
        return
    
    if a==15:
        pm.enter = True
        pm.enter_main = True
        pm.enter_other = False
        cfg.enter_pressed_seq = []
        return
        
    if scroll_str in st:
        scroll_ini()
        pm.scroll_main = True
        pm.scroll = True
        pm.scroll_other = False
        pm.scroll_other_mid = False
        return
    
    if 'alt' in st:
        alt_ini(st)
        pm.alt = True
        pm.alt_main = True
        pm.alt_double_trigger = False

    if 'ctrl' in st:
        ctrl_ini(st)
        pm.ctrl = True
        pm.ctrl_pressed = True
        pm.ctrl_main  = True
        pm.ctrl_has_chu = False
        pm.ctrl_co = 0
        if cfg.lock:
            pm.ctrl_lock = True
        else:
            pm.ctrl_lock = False

def deal_has_mode(st,a):
    if a==11:        
        pm.add_main = False        
        return
    
    if a==15:        
        pm.enter_main = False        
        return
        
    if scroll_str in st:
        pm.scroll_main = False        
        return
    
    if 'alt' in st:                
        pm.alt_main = False
        pm.alt_on_ctrl =False        

    if 'ctrl' in st:                
        pm.ctrl_main = False        

def ctrl_lock_alt_ini(st):
    cfg.lock_xing_mode = 1
    if ctrl_into_jia and 'alt_l' in st:
        cfg.lock_xing_mode = 2

def deal_p_key(st,a):
    if pm.noMode():
        deal_no_mode(st,a)

    elif 'alt' in st and pm.ctrl and not pm.ctrl_lock:
        pm.clearMode()
        pm.ctrl_alt = True
        pm.alt_main =False
        pm.alt_on_ctrl = True
        ctrl_lock_alt_ini(st)
    else:
        deal_has_mode(st,a)

def press_when_add(st,a):
    if a==11:
        return
    pm.add_other = True
    deal_add_pressed(st,a)

def press_when_enter(st,a):
    if a==15:
        return
    pm.enter_other = True
    deal_enter_pressed(st, a)

def press_when_scroll(st,a):
    if scroll_str in st:
        return
    pm.scroll_other = True
    deal_scroll_pressed(st, a)    

def press_when_ctrl_no_lock(st,a):    
    r = deal_lock_outer(st,a)
    cfg.lock_keep_step = True    
    return     

def press_when_ctrl_lock(st,a):    
    r = deal_lock_outer(st,a)
    return     

def press_when_alt(st,a):
    deal_alt_mode(a,st)

def press_when_ctrl_alt(st,a):
    if a==13:
        a = 10
    
    r = deal_lock_outer(st,a)
    return     

def press_when_mode(st,a):
    if pm.add:
        press_when_add(st,a)
        return
    
    if pm.enter:
        press_when_enter(st,a)
        return
    
    if pm.scroll:
        press_when_scroll(st,a)
        return
    
    if pm.ctrl:
        if 'caps' in st:          
            ctrl_cap()
            return
        if not pm.ctrl_lock:
            return press_when_ctrl_no_lock(st,a)
        return press_when_ctrl_lock(st,a)
    
    if pm.alt:
        press_when_alt(st,a)
        return
    
    if pm.ctrl_alt:
        press_when_ctrl_alt(st,a)

def press_no_mode(st,a,isDouble=False):
    if cfg.middle_pressed and scroll_str in st:    
        no_to_3()
        cfg.middle_pressed_other = True
        return
    if cfg.x2_pressed and scroll_str in st:
        xscro()
        cfg.x2_pressed_then_other = True
        return
    
    if cfg.x2_mode:
        return deal_key_x2_mode(st,a)
    
    if cfg.cmd_mode:    
        cfg.cmd_mode = deal_cmd(st, a)
        return
    
    if cfg.set_mode:      
        cfg.set_mode = deal_set(st, a)        
        return            

    if pause_str in st:        
        deal_pause()
        return
    
    if scroll_str in st:
        do_scroll_release(not st==scroll_str)
        
    if 'caps_lock' in st:        
        if not isDouble:
            if cfg.ctrlcap:
                mouse.Controller().press(mouse.Button.left)  
            else:
                deal_pressed()
        return        
       
    if a == 16:
        if not cfg.lock and  cfg.sum != '0':  
            if cfg.sum=='77':
                cfg.acc_add=0
                cfg.force_acc= not cfg.force_acc
                cfg.can_set_acc=True
                print('force acc',cfg.force_acc)         
            if cfg.sum=='88':                
                cfg.mouse_p_enable= not cfg.mouse_p_enable                
                print('mouse_p_enable',cfg.mouse_p_enable)         
            if len(cfg.sum)==5 and cfg.sum[:3]=='222' and '.' not in cfg.sum:
                cfg.jump_len = int(float(cfg.sum[3:]))
                print('set jump_len',cfg.jump_len)        
            set_cfg_sum_0()              
            cfg.capsShow = False
            cfg.add_show.append(-1)                     
            return
        
        if not cfg.lock:
            clear_lock()
            cfg.lock = True
            if not cfg.lock_keep_step_ori:
                cfg.lock_step = 0
            cfg.lock_step = 0
            return
        if cfg.sum!='0':
            cfg.sum='0'
            return
        
        if cfg.lock_xing_mode!=0:
            cfg.lock_xing_mode=0            
            return

        cfg.lock =False
        return
    
    if is_sca() and a==0:
        on_press_inner1(st,a)
        return
    
    if cfg.lock :        
        r = deal_lock_outer(st,a)
        cfg.lock_xing_mode_one_time = False
        return     
    
    if a==11:               
        deal_add()
        return

    if a ==15 :                
        if cfg.sum=='0.':
            enter_kuai()
            set_cfg_sum_0()
            return
        if cfg.sum=='0.0':
            enter_kuai2()
            set_cfg_sum_0()
            return
        
        deal_e(True,2)
        return
    
    if a>=0 and a<=15:
        on_press_inner1(st,a)
        return
    
def check_double(st,a):
    if pm.ctrl_pressed and ((a>=0 and a<=10) or a in (11,13,14,15)):
        pm.ctrl_other = True 
    if pm.ctrl_pressed and 'ctrl' not in st:
        pm.ctrl_any_other = True

    r = False
    if 'cmd' in st:
        pm.cmd_pressed = True
        pm.cmd_time = time.time()
    if 'caps' in st:
        if pm.cap_pressed:
            return True
        pm.cap_pressed = True
        return False
    
    if a==11:
        if pm.add_pressed:
            r = True
            if cfg.w2showing and not cfg.x2_mode and cfg.sum=='0':
                cfg.sum='1'
                xing()
            if pm.add_main:
                pm.add_other = True
        pm.add_pressed = True
        return r
    if a==15:
        if pm.enter_pressed:
            r = True
            if pm.enter_main:
                pm.enter_other = True
        else:
            cfg.jump_dx=[0,0]            
        pm.enter_pressed = True        
        return r
    
    if scroll_str in st:
        if pm.scroll_pressed:
            r = True
            if pm.scroll_main:
                pm.scroll_other = True
        pm.scroll_pressed = True
        return r
    
    if 'ctrl' in st:
        if pm.ctrl_pressed:
            r = True
        else:
            pm.ctrl_other = False
            pm.ctrl_any_other = False

        pm.ctrl_pressed = True
        return r
    
    if 'alt' in st:
        if pm.alt_pressed:
            r = True
            if pm.alt and not pm.alt_double_trigger:
                pm.alt_double_trigger = True
        pm.alt_pressed = True
        return r

def on_press(key):    
    global ctrl_co    
    cfg.last_active_time = time.time()
    cfg.key_input_time = time.time()    
    st = str(key)
    a = decode(key, True)    
    if 'ctrl' in st:
        cfg.can_set_acc=True
        cfg.acc_no_reset=True
    if not scroll_str in st:
        clear_sign()        
        
    if a==-2:
        return
    cfg.lock_keep_step_ori = cfg.lock_keep_step
    if 'ctrl' not in st:
        cfg.lock_keep_step = False    
    if time.time()-cfg.x2_mode_exit_time>0.01:
        cfg.x2_mode_exit_time = 0
        
    if 'ctrl' in st  and g_ctrl2_enable:
        check_ctrl2()

    if a==-1 and scroll_str not in st and cfg.x2_mode and 'ctrl_l' not in st and pause_str not in st and \
        'caps' not in st:
        cfg.x2_mode = False    
        clear(True) 

    if a==-1 and not is_my_key(st,a) :
        clear_lock()

    d = check_double(st,a)
    on_press2(st,a,d)
    cfg.last_mouse_pos = get_cur_pos()

def on_press2(st,a,isDouble):        
    if not is_my_key(st,a):
        return
    
    if a==11 and cfg.x2_mode:
        deal_key_x2_mode(st,a)
        return
    
    if is_p_key(st,a):
        if isDouble:
            return        
        deal_p_key(st,a)

    if cfg.x2_pressed and a>0:
        pm.add_other = True
        cfg.x2_pressed_then_other = True
        deal_scroll_pressed(st,a)
        return
    
    if not pm.noMode():
        press_when_mode(st,a)
        return    
    
    press_no_mode(st,a,isDouble)
        
def add_release(st,a):
    if not pm.add_main:
        return
    if pm.add_other:
        return
    press_no_mode(st,a)

def enter_release(st,a):
    if not pm.enter_main:
        return
    if pm.enter_other:
        return    
    press_no_mode(st,a)

def scroll_release(st,a):
    if not pm.scroll_main:
        return
    if pm.scroll_other or pm.scroll_other_mid:
        set_cfg_sum_0()
        return    
    press_no_mode(st,a)

def set_unPressed(st,a):   
    if 'cmd' in st:
        pm.cmd_pressed = False
        pm.cmd_time = time.time()
    if 'caps' in st:
        pm.cap_pressed = False
        if cfg.ctrlcap:
            mouse.Controller().release(mouse.Button.left)  

    if a==11:    
        pm.add_pressed = False   
        acc_down(True)
        
    if a==15:    
        pm.enter_pressed = False    
        cfg.jump_dx=[0,0]
    if scroll_str in st:    
        pm.scroll_pressed = False    
        pm.scroll_re_time=time.time()
        pm.scroll_re_dx=0
        if pm.scroll_other:        
            cfg.last_move_is_right_solo=False

    if 'ctrl' in st:    
        pm.ctrl_pressed = False    
        if cfg.ctrl2_job and time.time()-cfg.ctrl2_time<x_cancel_gap and not cfg.ctrl2_other:
            do_ctrl2()
        cfg.ctrl2_job = False
    if 'alt' in st:    
        pm.alt_pressed = False

def lock_ctrl_release(st,a):
    if cfg.lock_xing_mode==1:
        xing_mode_enter()
    cfg.lock_xing_mode = 0    

def alt_main_release(st,a):
    alt_fire()
    cfg.alt_mode = 0    

def alt_on_ctrl_release(st,a):
    if cfg.lock_xing_mode==1:
        xing_mode_enter()
    cfg.lock_xing_mode = 0    

def on_release(key):
    st = str(key)
    a = decode(key, False)        
    if a=='-2':
        return
    
    on_release2(st,a)
    set_unPressed(st,a)

def on_release2(st,a):    
    cfg.key_input_time = time.time()    
    if a==11 and cfg.x2_mode:
        return
    
    if not is_my_key(st,a):
        return
    if not is_p_key(st,a):
        return
    
    if a==11:
        pm.add = False
        add_release(st,a)
        return
    
    if a==15:
        pm.enter =False
        enter_release(st,a)
        return
    
    if scroll_str in st:
        pm.scroll = False
        scroll_release(st,a)
        return
    
    if 'ctrl' in st:
        if pm.ctrl or pm.ctrl_alt:
            pm.clearMode()

        if pm.alt:
            alt_fire3()
            return
        
        if not pm.ctrl_main:
            return
        
        if pm.ctrl_lock:
            lock_ctrl_release(st,a)
        else:
            if cfg.lock_xing_mode!=0 or (pm.ctrl_has_chu and pm.ctrl_co>1) or \
                (not pm.ctrl_has_chu and pm.ctrl_co==1):
                cfg.lock = True                
        return
    
    if 'alt' in st:
        pm.alt =False
        if pm.alt_main:
            alt_main_release(st,a)
            return
        
        if not pm.alt_on_ctrl:
            return
        
        alt_on_ctrl_release(st,a)

        if pm.ctrl_alt:
            pm.clearMode()
            pm.ctrl = True
    